/**
 * Created by jiachenpan on 16/11/18.
 */

const validUsername = (str) => {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

function valid(reg) {
  return function (str) {
    reg.lastIndex = 0
    return reg.test(str)
  }
}

/* 合法uri*/
const validateURL = valid(
  /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
)

/* 小写字母*/
const validateLowerCase = valid(/^[a-z]+$/)

/* 大写字母*/
const validateUpperCase = valid(/^[A-Z]+$/)

/* 大小写字母*/
const validateAlphabets = valid(/^[A-Za-z]+$/)

// 数量 带.
const validateCount = valid(/^\d+(?=\.{0,1}\d+$|$)/)

// 数字
const validateNumber = valid(/^[0-9]+$/)

// 数字和字母
const validateNumberAndLetter = valid(/^[A-Za-z0-9]+$/)

// 校验邮箱
const validateEmail = valid(
  /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
)

// 校验手机号码
const validateMobilePhone = valid(
  /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/g
)

// 校验电话号码
const validateTelephone = valid(/\d{3}-\d{8}|\d{4}-\d{7}/g)

// 校验联系方式(手机号码或电话号码)
const validateContactsTel = (str) => {
  return validateMobilePhone(str) || validateTelephone(str)
}

// 校验营业执照编号/统一社会信用代码
const validateUscId = valid(
  /^([0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}|[1-9]\d{14})$/
)

// 校验内地身份证
const validIdCard = valid(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/)

// 校验香港身份证
const validHongKongCard = valid(/^[HMhm]{1}([0-9]{10}|[0-9]{8})$/)

// 校验身份证
const validateIdCard = (str) => {
  return validIdCard(str) || validHongKongCard(str)
}

// 校验营业执照/统一社会信用代码/身份证
const validateIdCardAndUscId = (str) => {
  return validIdCard(str) || validHongKongCard(str) || validateUscId(str)
}

// 新能源车牌校验
const EnergyVehicleReg =
  /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/
const validateNewEnergyVehicle = valid(EnergyVehicleReg)

// 普通车牌号校验
const vehicleReg =
  /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/
const validateNormalVehicle = valid(vehicleReg)

// 校验车牌号
const validateVehicle = (vehicleNumber) => {
  return (
    validateNormalVehicle(vehicleNumber) ||
    validateNewEnergyVehicle(vehicleNumber)
  )
}

export {
  validUsername,
  validateURL,
  validateLowerCase,
  validateUpperCase,
  validateAlphabets,
  validateEmail,
  validateMobilePhone,
  validateTelephone,
  validateContactsTel,
  validateUscId,
  validIdCard,
  validHongKongCard,
  validateIdCard,
  validateNormalVehicle,
  validateNewEnergyVehicle,
  validateVehicle,
  validateIdCardAndUscId,
  validateNumberAndLetter,
  validateNumber,
  validateCount
}
